﻿
Imports std

Public Module wrapperManager
    Private ReadOnly triggers As vector(Of pair(Of _do(Of configParser, String, Boolean), 
                                                   _do(Of islice, islice))) = Nothing

    Sub New()
        Const enableUnhealthWrapper As String = "enableUnhealthWrapper"
        triggers = New vector(Of pair(Of _do(Of configParser, String, Boolean), _do(Of islice, islice)))()
        register("enableCache",
                 Function(ByRef s As islice) As islice
                     If istype(Of memslice)(s) Then
                         Return s
                     Else
                         Return New cached_slice_wrapper(s)
                     End If
                 End Function)
        register(Function(ByRef s As islice) As islice
                     Return New locked_slice_wrapper(s)
                 End Function)
        register(enableUnhealthWrapper,
                 Function(ByRef s As islice) As islice
                     Return New unhealth_slice_wrapper(s, True)
                 End Function)
        register_not(enableUnhealthWrapper,
                     Function(ByRef s As islice) As islice
                         Return New unhealth_slice_wrapper(s, False)
                     End Function)
        register(Function(ByRef s As islice) As islice
                     Return New result_initialize_slice_wrapper(s)
                 End Function)
    End Sub

    Private Sub register(ByVal c As _do(Of configParser, String, Boolean), ByVal d As _do(Of islice, islice))
        assert(Not c Is Nothing)
        assert(Not d Is Nothing)
        triggers.push_back(make_pair(c, d))
    End Sub

    Private Sub register(ByVal d As _do(Of islice, islice))
        register(Function(ByRef c As configParser, ByRef n As String) As Boolean
                     Return True
                 End Function,
                 d)
    End Sub

    Private Function config(ByVal s As String,
                            Optional ByVal reverse As Boolean = False) As _do(Of configParser, String, Boolean)
        Return Function(ByRef c As configParser, ByRef n As String) As Boolean
                   Dim rtn As Boolean = False
                   rtn = c.parameter(n, s, False)
                   If reverse Then
                       Return Not rtn
                   Else
                       Return rtn
                   End If
               End Function
    End Function

    Private Sub register(ByVal s As String, ByVal d As _do(Of islice, islice))
        register(config(s), d)
    End Sub

    Private Sub register_not(ByVal s As String, ByVal d As _do(Of islice, islice))
        register(config(s, True), d)
    End Sub

    Public Function wrap(ByVal s As islice, ByVal config As configParser, ByVal name As String) As islice
        For i As Int64 = 0 To triggers.size() - 1
            If triggers(i).first(config, name) Then
                s = triggers(i).second(s)
            End If
        Next
        Return s
    End Function
End Module
